﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Typing_GUI.CLASS
{
    public class CDiem
    {
        #region"Thuoc tinh"
            private int mabaitap;
            private int manguoidung;
            private int sodiem;
            private int soloisai;
            private int thoigian;
            private string tocdo;

            
        #endregion

        #region"properties"
            public int Thoigian
            {
                get { return thoigian; }
                set { thoigian = value; }
            }

            public int Soloisai
            {
                get { return soloisai; }
                set { soloisai = value; }
            }
            public int Sodiem
            {
                get { return sodiem; }
                set { sodiem = value; }
            }

            public int Manguoidung
            {
                get { return manguoidung; }
                set { manguoidung = value; }
            }
            public int Mabaitap
            {
                get { return mabaitap; }
                set { mabaitap = value; }
            }
            public string Tocdo
            {
                get { return tocdo; }
                set { tocdo = value; }
            }
        #endregion

        #region"phuong thuc"

        public bool createMatch()
        {
            string sql = "insert into Diem(MaNguoiDung,MaBaiTap,SoLoiSai,SoDiem,TocDo,ThoiGian) values";
            sql += "(" + this.manguoidung.ToString() + "," + this.mabaitap.ToString();
            sql+="," + this.soloisai.ToString()+","+this.sodiem.ToString();
            sql += "," + this.tocdo + "," + this.thoigian.ToString() + ")";
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        public bool deleteMatch()
        {
            string sql = "delete from Diem where MaNguoiDung=" + this.manguoidung.ToString();
            sql += " and MaBaiTap=" + this.mabaitap.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        

        public bool updateMatch()
        {
            string sql = "update Diem set ";
            sql += "SoLoiSai=" + this.soloisai.ToString() + ",SoDiem=" + this.sodiem.ToString();
            sql += ",TocDo=" + this.tocdo + ",ThoiGian=" + this.thoigian.ToString();
            sql += " where MaNguoiDung=" + this.manguoidung.ToString() + " and";
            sql += " MaBaiTap=" + this.mabaitap.ToString();
            if (!DataProvider.ExecuteNonQuery(sql))
                return false;
            return true;
        }

        public static bool ExistMatch(int userID,int lessoID)
        {
            string sql = "select*from Diem where MaNguoiDung=" + userID.ToString() + " and MaBaiTap=" + lessoID.ToString();
            System.Data.DataTable dt = DataProvider.ExecuteQuery(sql);
            if (dt.Rows.Count > 0)
                return true;
            return false;
        }
        //Ham tinh tong Diem cua nguoi dung
        public static int SumMatch(int userID)
        {
            int sum = 0;
            string sql = "select SoDiem from Diem where MaNguoiDung=" + userID.ToString();
            System.Data.DataTable dt = DataProvider.ExecuteQuery(sql);
            if (dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    sum+=int.Parse(dr[0].ToString());
                }
            }
            return sum;
        }
        //Hàm lấy 1 CDiem 
        public static CDiem getMatch(int userID, int lessonID)
        {
            CDiem m = new CDiem();
            string sql = "select * from Diem where MaNguoiDung=" + userID.ToString() + " and MaBaiTap=" + lessonID.ToString();
            System.Data.DataTable dt = DataProvider.ExecuteQuery(sql);
            if (dt.Rows.Count < 1)
            {
                return null;
            }
            System.Data.DataRow dr = dt.Rows[0];
            m.mabaitap = int.Parse(dr["MaBaiTap"].ToString());
            m.manguoidung = int.Parse(dr["MaNguoiDung"].ToString());
            m.soloisai = int.Parse(dr["SoLoiSai"].ToString());
            m.sodiem = int.Parse(dr["SoDiem"].ToString());
            m.tocdo = dr["TocDo"].ToString();
            m.thoigian = int.Parse(dr["ThoiGian"].ToString());
            return m;
        }
        #endregion
        }
}
